package org.sais.eswn.deck;
/*
 * DeckEditorWindow.java
 *
 * Created on 2007/2/7 10:38
 */

import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.event.DocumentListener;
import org.sais.eswn.card.Card;
import org.sais.eswn.tool.Tools;
import org.sais.eswn.card.cardlabel.UniLabel;
import org.sais.eswn.tool.MyListModel;
import org.sais.eswn.ui.ListItem;
import org.sais.eswn.ui.MyCellRenderer;

/**
 *
 * @author  Romulus
 */
public class DeckEditorWindow extends javax.swing.JFrame {

    private JCheckBox[] colors;
    private JCheckBox[] atkattrs;
    private JCheckBox[] defattrs;
    private ArrayList<String> mCardList = new ArrayList<String>();
    private ArrayList<String> mDeckList = new ArrayList<String>();
    private Deck deck;
    private MouseListener clistClickAdapter;
    private MouseListener decklistClickAdapter;
    private String mDeckPath = null;
    String version;

    public DeckEditorWindow(String version) throws Exception {
        initComponents();
        //this.setIconImage(Toolkit.getDefaultToolkit().createImage(new ReadInnerFile("deckeditoricon.jpg").u));
        this.version = version;
        Initilize();
        loadList();
        applyFilters();
        showDeckList(-1);
        amount.setText(_clist.getModel().getSize() + Tools.getLoc(9));

        //this.setSize(800, 600);
        this.setVisible(true);

    }

    void Initilize() {
/*        colors = new JCheckBox[]{_spellblue, _spellgreen, _spellred};
        atkattrs = new JCheckBox[]{_atka, _atkb, _atkc};
        defattrs = new JCheckBox[]{_defa, _defb, _defc};

        for (JCheckBox j : atkattrs) {
            j.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    applyFilters();
                }
            });
        }
        for (JCheckBox j : defattrs) {
            j.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    applyFilters();
                }
            });
        }

        for (JCheckBox j : colors) {
            j.addActionListener(new java.awt.event.ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    applyFilters();
                }
            });
        }*/

        _textfilter.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                applyFilters();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                applyFilters();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                applyFilters();
            }
        });

        _clistsort.add(_clistsortno);
        _clistsort.add(_clistsortcolor);
    }

    void ShowCard(String cardno) {   //Translate flaged text into colored text
        Tools.showCard(cardno, _SCArea);
    }

    void loadList() {
        mCardList.addAll(Tools.listCardNos());
    }

    void showFilteredList() {
        MyListModel mod = new MyListModel();
        mod.removeAllElements();
        _clist.setModel(mod);
        _clist.setCellRenderer(new MyCellRenderer());
        ListSelectionListener[] listeners = _clist.getListSelectionListeners();
        for (int k = 0; k < listeners.length; ++k) {
            _clist.removeListSelectionListener(listeners[k]);
        }

        for (String no : mCardList) {
            Card c = Tools.getCard(no);
            mod.addElementwithoutFire(new ListItem(c.getDisplayColor(), c.name, new UniLabel(null, no, false)));
        }
        mod.fire();
        for (int k = 0; k < listeners.length; ++k) {
            _clist.addListSelectionListener(listeners[k]);
        }
        _clist.setModel(mod);
        MouseListener[] ms = _clist.getMouseListeners();
        for (int i = 0; i < ms.length; ++i) {
            if (ms[i].equals(clistClickAdapter)) {
                _clist.removeMouseListener(ms[i]);
            }
        }
        clistClickAdapter = new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dispatchShowCard(_clist, evt);
                if (_clist.locationToIndex(evt.getPoint()) >= 0) {
                    synchronizeList(0);
                }
                if (evt.getClickCount() >= 2) {
                    addtoDeck();
                }
            }
        };
        _clist.addMouseListener(clistClickAdapter);
    }

    void dispatchShowCard(JList list, MouseEvent evt) {
        int index = list.locationToIndex(evt.getPoint());
        if (index < 0) {
            return;
        }
        String cardno = ((ListItem) (list.getModel().getElementAt(index))).label.cardno;
        ShowCard(cardno);
    }

    void showSelected(JList list) {
        int index = list.getSelectedIndex();
        String cardno = ((ListItem) (list.getModel().getElementAt(index))).label.cardno;
        if (index >= 0) {
            ShowCard(cardno);
        }
    }

    void showDeckList(int setindex) {
        sortDeckList();

        DefaultListModel mod = new DefaultListModel();
        mod.removeAllElements();
        _decklist.setModel(mod);
        _decklist.setCellRenderer(new MyCellRenderer());
        for (String no : mDeckList) {
            Card c = Tools.getCard(no);
            mod.addElement(new ListItem(c.getDisplayColor(), c.name, new UniLabel(null, no, false)));
        }
        _decklist.setModel(mod);
        MouseListener[] ms = _decklist.getMouseListeners();
        for (int i = 0; i < ms.length; ++i) {
            if (ms[i].equals(decklistClickAdapter)) {
                _decklist.removeMouseListener(ms[i]);
            }
        }
        decklistClickAdapter = new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dispatchShowCard(_decklist, evt);
                if (_decklist.locationToIndex(evt.getPoint()) >= 0) {
                    synchronizeList(1);
                }
                if (evt.getClickCount() >= 2) {
                    removefromDeck();
                }
            }
        };
        _decklist.addMouseListener(decklistClickAdapter);
        if (setindex >= 0) {
            _decklist.setSelectedIndex(setindex);
        }
/*
        int[] fires = new int[]{ 3, 3, 3 };
        int[] dodges = new int[]{ 3, 3, 3 };
        for (String no : mDeckList) {
            Card c = Tools.getCard(no);
            fires[c.text.getFire().getValue() - 1]--;
            dodges[c.text.getDodge().getValue() - 1]--;
        }

        String fireleft = "";
        for (int k=0;k<fires.length;++k) {
            if (fires[k] < 0) {
                fireleft = Tools.getLoc(15);
                break;
            }
            for (int i=0;i<fires[k];++i) {
                fireleft += (char) ('A' + k);
            }
        }
        String dodgeleft = "";
        for (int k=0;k<dodges.length;++k) {
            if (dodges[k] < 0) {
                dodgeleft = Tools.getLoc(15);
                break;
            }
            for (int i=0;i<dodges[k];++i) {
                dodgeleft += (char) ('A' + k);
            }
        }

        _decksab.setText(Tools.parseLocale(16, new String[]{fireleft}));
        _decksabline2.setText(Tools.parseLocale(17, new String[]{dodgeleft}));*/
    }

    private void sortDeckList() {
        if (_clistsortcolor.isSelected()) {
            Collections.sort(mDeckList, new Comparator<String>() {

                public int compare(String o1, String o2) {
                    Card c1 = Tools.getCard(o1);
                    Card c2 = Tools.getCard(o2);
                    if (c1.getColor() != c2.getColor()) {
                        return c1.getColor() - c2.getColor();
                    }
                    return c1.getNumber() - c2.getNumber();
                }
            });
        } else if (_clistsortno.isSelected()) {
            Collections.sort(mDeckList, new Comparator<String>() {

                public int compare(String o1, String o2) {
                    Card c1 = Tools.getCard(o1);
                    Card c2 = Tools.getCard(o2);
                    return c1.getNumber() - c2.getNumber();
                }
            });
        }

    }

    private void synchronizeList(int acter) {   //0:CardList 1:DeckList
        JList act;
        JList sync;
        ArrayList<String> actlist;
        ArrayList<String> synclist;
        if (acter == 0) {
            act = _clist;
            actlist = mCardList;
            sync = _decklist;
            synclist = mDeckList;
        } else {
            act = _decklist;
            actlist = mDeckList;
            sync = _clist;
            synclist = mCardList;
        }
        if (act.getSelectedIndex() < 0) {
            return;
        }
        int index = synclist.indexOf(actlist.get(act.getSelectedIndex()));
        if (index >= 0) {
            sync.setSelectedIndex(index);
            sync.ensureIndexIsVisible(index);
        }
    }

    void applyFilters() {
        /*if (colors == null) {
            return;
        }*/
        mCardList = new ArrayList<String>();
        for (String no : Tools.listCardNos()) {
            mCardList.add(no);
        }
        /*
        applyColorFilter();
        applyAttrFilter();
        applyTextFilter();
         * */
        sortCardList();
        showFilteredList();
        amount.setText(_clist.getModel().getSize() + Tools.getLoc(9));
    }

    //Start of Filters
    /*
    private void applyColorFilter() {
    mCardList = new ArrayList<String>();

    for (int i = 0; i < colors.length; ++i) {
    if (colors[i].isSelected()) {
    fillColorList(i, mCardList);
    }
    }
    }

    private void applyAttrFilter() {
    ArrayList<String> list = new ArrayList<String>();
    for (String no : mCardList) {
    Card c = Tools.getCard(no);
    boolean fit = true;
    for (int i = 0; i < atkattrs.length; ++i) {
    if (atkattrs[i].isSelected()) {
    if (!c.text.getFire().hasBullet(i)) {
    fit = false;
    break;
    }
    }
    if (defattrs[i].isSelected()) {
    if (!c.text.getConquer().hasBullet(i)) {
    fit = false;
    break;
    }
    }
    }
    if (fit) {
    list.add(no);
    }
    }
    mCardList.clear();
    mCardList.addAll(list);
    }


    private void fillColorList(int index, ArrayList<String> list) {
    int color = Card.COLOR_WHITE;
    switch (index) {
    case 0:
    color = Card.COLOR_WHITE;
    break;
    case 1:
    color = Card.COLOR_BLUE;
    break;
    case 2:
    color = Card.COLOR_BLACK;
    break;
    case 3:
    color = Card.COLOR_RED;
    break;
    case 4:
    color = Card.COLOR_GREEN;
    break;
    default:
    break;
    }

    for (String no : Tools.listCardNos()) {
    if (Tools.getCard(no).getColor() == color) {
    list.add(no);
    }
    }
    }

    private void applyTextFilter() {
    String filter = _textfilter.getText();
    if (filter.length() == 0) {
    return;
    }
    ArrayList<String> newlist = new ArrayList<String>();
    for (String no : mCardList) {
    if (Tools.getCard(no).text.getRuleText().contains(filter)) {
    newlist.add(no);
    }
    }
    mCardList.clear();
    mCardList.addAll(newlist);
    }
     */
    private void sortCardList() {
        if (_clistsortcolor.isSelected()) {
            Collections.sort(mCardList, new Comparator<String>() {

                public int compare(String o1, String o2) {
                    Card c1 = Tools.getCard(o1);
                    Card c2 = Tools.getCard(o2);
                    if (c1.getColor() != c2.getColor()) {
                        return c1.getColor() - c2.getColor();
                    }
                    return c1.getNumber() - c2.getNumber();
                }
            });
        } else if (_clistsortno.isSelected()) {
        }
    }

    private void addtoDeck() {
        if (_clist.getSelectedIndex() < 0) {
            return;
        }
        String no = mCardList.get(_clist.getSelectedIndex());
        if (!mDeckList.contains(no)) {
            mDeckList.add(no);
        }
        showDeckList(-1);
        synchronizeList(0);
    }

    private void removefromDeck() {
        int index = _decklist.getSelectedIndex();
        if (index < 0) {
            return;
        }
        mDeckList.remove(index);
        showDeckList(index);
        synchronizeList(1);
    }

    void newDeck() {
        mDeckList.clear();
        _deckname.setText("New Deck");
        deck = new Deck(_deckname.getText(), new ArrayList<String>());
        showDeckList(-1);
    }

    void loadDeck() {
        JFileChooser chooser = new JFileChooser();
        File f;
        String sdir = Tools.readConfig("lastdeckfolder");
        if (sdir.equals("")) {
            sdir = "deck";
        }
        if (new File(sdir).exists()) {
            f = new File(sdir);
        } else {
            f = new File(".");
        }
        chooser.setCurrentDirectory(f);
        chooser.setAcceptAllFileFilterUsed(true);
        chooser.addChoosableFileFilter(new decFilter());

        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        }
        try {
            newDeck();
            File file = chooser.getSelectedFile();
            Tools.updateConfig("lastdeckfolder", file.getParent());
            mDeckPath = file.getPath();
            deck = Deck.load(file);
            _deckname.setText(deck.getDeckname());
            mDeckList.addAll(deck.listCardnos());
            showDeckList(-1);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, Tools.getLoc(10));
        }
    }

    boolean checkDeck() {
        return true;
    }

    boolean checkCharCard(int cardnum) {
        if (cardnum % 100 == 0 || cardnum == 8001 || cardnum == 8002 || cardnum == 8101) {
            return true;
        } else {
            return false;
        }
    }

    void saveDeck() {
        if (!checkDeck()) {
            return;
        }
        JFileChooser chooser = new JFileChooser();
        String sdir = Tools.readConfig("lastdeckfolder");
        if (sdir.equals("")) {
            sdir = "deck";
        }
        if (!new File(sdir).exists()) {
            new File(sdir).mkdir();
        }
        chooser.setCurrentDirectory(new File(sdir));
        chooser.setAcceptAllFileFilterUsed(true);
        chooser.addChoosableFileFilter(new decFilter());
        if (mDeckPath == null) {
            chooser.setSelectedFile(new File(_deckname.getText() + ".dec"));
        } else {
            chooser.setSelectedFile(new File(mDeckPath));
        }
        int result = chooser.showSaveDialog(this);
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        }
        if (chooser.getFileFilter() instanceof decFilter) {
            //Deck File
            //Convert dlist to deck
            deck = new Deck();
            try {
                File file = chooser.getSelectedFile();
                if (file.getName().length() < 4) {
                    file = new File(file.getPath() + ".dec");
                } else if (!file.getName().substring(file.getName().length() - 4).endsWith(".dec")) {
                    file = new File(file.getPath() + ".dec");
                }
                Tools.updateConfig("lastdeckfolder", file.getParent());
                mDeckPath = file.getPath();
                deck.setDeckname(_deckname.getText());
                for (String no : mDeckList) {
                    deck.add(no);
                }
                deck.save(file);
                JOptionPane.showMessageDialog(null, file.getName() + Tools.getLoc(11));
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, Tools.getLoc(10));
            }
        } else {
            //Plain Text File
            try {
                File file = chooser.getSelectedFile();
                if (file.getName().length() < 4) {
                    file = new File(file.getPath() + ".txt");
                } else if (!file.getName().substring(file.getName().length() - 4).endsWith(".txt")) {
                    file = new File(file.getPath() + ".txt");
                }
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
                for (int i = 0; i < _decklist.getModel().getSize(); ++i) {
                    out.write(((ListItem) (_decklist.getModel().getElementAt(i))).getValue() + "\r\n");
                }
                out.close();
                JOptionPane.showMessageDialog(null, file.getName() + Tools.getLoc(11));
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(this, Tools.getLoc(10));
            }
        }
    }

    void showTextDeck() {
        JTextArea jmessage = new JTextArea("");
        JScrollPane js = new JScrollPane(jmessage);
        js.setPreferredSize(new Dimension(100, 350));

        String message = "";
        for (int i = 0; i < _decklist.getModel().getSize(); ++i) {
            message += (((ListItem) (_decklist.getModel().getElementAt(i))).getValue() + "\r\n");
        }
        jmessage.setText(message);

        JOptionPane.showMessageDialog(this, js, "TEXT FORM DECK", JOptionPane.INFORMATION_MESSAGE);
    }
    /*
    void transfer() {
    JTextArea jdeck = new JTextArea("");
    JScrollPane js = new JScrollPane(jdeck);
    js.setPreferredSize(new Dimension(100, 350));
    JOptionPane.showMessageDialog(this, js, "TRANSFER FROM TEXT", JOptionPane.QUESTION_MESSAGE);
    String txt = jdeck.getText();
    if (txt.length() < 5) {
    return;
    }

    newDeck();
    String[] lines = txt.split("\n");
    for (String line : lines) {
    String[] s;
    if (line.startsWith("Leader")) {
    s = line.split(" ");
    if (s.length == 4) {
    s[2] += " " + s[3];
    }
    int cardnum = findCard(s[2]);
    if (cardnum > 0) {
    leader = cardnum;
    dlist[dlistcnt] = cardnum;
    camount[cardnum] += Tools.safeParseInt(s[1].substring(2, 3));
    ++dlistcnt;
    }
    } else if (line.startsWith("Lv")) {
    s = line.split(" ");
    if (s.length == 3) {
    s[1] += " " + s[2];
    }
    int cardnum = findCard(s[1]);
    if (cardnum > 0) {
    dlist[dlistcnt] = cardnum;
    camount[cardnum] += Tools.safeParseInt(s[0].substring(2, 3));
    ++dlistcnt;
    }
    } else if (line.length() > 1) {
    if (line.charAt(1) == 'x') {
    s = line.split(" ");
    if (s.length == 3) {
    s[1] += " " + s[2];
    }
    int cardnum = findCard(s[1]);
    if (cardnum > 0) {
    dlist[dlistcnt] = cardnum;
    camount[cardnum] += Tools.safeParseInt(s[0].substring(0, 1));
    ++dlistcnt;
    }
    } else if (line.charAt(2) == 'x') {
    //Wiki Form
    s = line.split(" ");
    int cardnum = findCard(s[2].substring(1, s[2].length() - 1));
    if (cardnum > 0) {
    dlist[dlistcnt] = cardnum;
    camount[cardnum] += Tools.safeParseInt(s[0]);
    ++dlistcnt;
    }
    }
    }
    }

    showDeckList(-1);
    }

    int findCard(String cardname) {
    for (Card c : Tools.cdata) {
    if (c == null) {
    continue;
    }
    if (cardname.equals(c.CardName)) {
    return c.CardNumber;
    }

    }
    return -1;
    }
     */

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _clistsort = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        _SCArea = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        _clist = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        _decklist = new javax.swing.JList();
        _deckname = new javax.swing.JTextField();
        _allcolorcheck = new javax.swing.JButton();
        _allcoloruncheck = new javax.swing.JButton();
        savedeck = new javax.swing.JButton();
        loaddeck = new javax.swing.JButton();
        newdeck = new javax.swing.JButton();
        amount = new javax.swing.JLabel();
        _decksab = new javax.swing.JLabel();
        _clistsortno = new javax.swing.JRadioButton();
        jLabel20 = new javax.swing.JLabel();
        _showtextdeck = new javax.swing.JButton();
        _transfer = new javax.swing.JButton();
        _decksabline2 = new javax.swing.JLabel();
        _clistsortcolor = new javax.swing.JRadioButton();
        _attrallcheck = new javax.swing.JButton();
        _attralluncheck = new javax.swing.JButton();
        _textfilter = new javax.swing.JTextField();
        _decksab1 = new javax.swing.JLabel();
        _reset = new javax.swing.JButton();
        jMenuBar6 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        _checkrule = new javax.swing.JMenuItem();
        _export = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        closeeditor5 = new javax.swing.JMenuItem();
        quit5 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("東方銀翼札デッキエディター");
        setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12)); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        _SCArea.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        jScrollPane1.setViewportView(_SCArea);

        _clist.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _clist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        _clist.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                _clistValueChanged(evt);
            }
        });
        _clist.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                _clistKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(_clist);

        _decklist.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _decklist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        _decklist.setVisibleRowCount(40);
        _decklist.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                _decklistValueChanged(evt);
            }
        });
        _decklist.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                _decklistKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(_decklist);

        _deckname.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _deckname.setText("NEW DECK");
        _deckname.setPreferredSize(new java.awt.Dimension(228, 19));
        _deckname.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                _decknameFocusGained(evt);
            }
        });

        _allcolorcheck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _allcolorcheck.setText("全て選択");
        _allcolorcheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _allcolorcheckActionPerformed(evt);
            }
        });

        _allcoloruncheck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _allcoloruncheck.setText("全て外す");
        _allcoloruncheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _allcoloruncheckActionPerformed(evt);
            }
        });

        savedeck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        savedeck.setText("SAVE");
        savedeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savedeckActionPerformed(evt);
            }
        });

        loaddeck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        loaddeck.setText("LOAD");
        loaddeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loaddeckActionPerformed(evt);
            }
        });

        newdeck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        newdeck.setText("NEW");
        newdeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newdeckActionPerformed(evt);
            }
        });

        amount.setFont(new java.awt.Font("MS PGothic", 0, 12));
        amount.setText("45枚");

        _decksab.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _decksab.setText("スペルコスト");

        _clistsortno.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _clistsortno.setText("No.");
        _clistsortno.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        _clistsortno.setMargin(new java.awt.Insets(0, 0, 0, 0));
        _clistsortno.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                _clistsortnoItemStateChanged(evt);
            }
        });

        jLabel20.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        jLabel20.setText("Sorted by:");

        _showtextdeck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _showtextdeck.setText("TEXT");
        _showtextdeck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _showtextdeckActionPerformed(evt);
            }
        });

        _transfer.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _transfer.setText("TRANSFER");
        _transfer.setEnabled(false);
        _transfer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _transferActionPerformed(evt);
            }
        });

        _decksabline2.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _decksabline2.setText("イベントコスト");

        _clistsortcolor.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _clistsortcolor.setSelected(true);
        _clistsortcolor.setText("色");
        _clistsortcolor.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        _clistsortcolor.setMargin(new java.awt.Insets(0, 0, 0, 0));
        _clistsortcolor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                _clistsortcolorItemStateChanged(evt);
            }
        });

        _attrallcheck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _attrallcheck.setText("全て選択");
        _attrallcheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _attrallcheckActionPerformed(evt);
            }
        });

        _attralluncheck.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _attralluncheck.setText("全て外す");
        _attralluncheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _attralluncheckActionPerformed(evt);
            }
        });

        _textfilter.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 14));
        _textfilter.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                _textfilterPropertyChange(evt);
            }
        });

        _decksab1.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _decksab1.setText("テキスト");

        _reset.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _reset.setText("リセット");
        _reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _resetActionPerformed(evt);
            }
        });

        jMenu1.setText("ルール確認");
        jMenu1.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));

        _checkrule.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        _checkrule.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _checkrule.setText("ルールページを開く");
        _checkrule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _checkruleActionPerformed(evt);
            }
        });
        jMenu1.add(_checkrule);

        jMenuBar6.add(jMenu1);

        _export.setText("Export");
        _export.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        _export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _exportActionPerformed(evt);
            }
        });

        jMenuItem1.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        jMenuItem1.setText("カードリスト");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        _export.add(jMenuItem1);

        jMenuBar6.add(_export);

        jMenu6.setText("終了");
        jMenu6.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));

        closeeditor5.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        closeeditor5.setText("デッキエディターを閉じる");
        closeeditor5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeeditorActionPerformed(evt);
            }
        });
        jMenu6.add(closeeditor5);

        quit5.setFont(new java.awt.Font("ＭＳ Ｐゴシック", 0, 12));
        quit5.setText("ゲーム終了");
        quit5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });
        jMenu6.add(quit5);

        jMenuBar6.add(jMenu6);

        setJMenuBar(jMenuBar6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel20)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_clistsortno)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_clistsortcolor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(amount))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_allcolorcheck)
                            .addComponent(_allcoloruncheck))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_textfilter, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(_decksab1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                                .addComponent(_reset))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(_attralluncheck)
                            .addComponent(_attrallcheck))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_decksab)
                    .addComponent(_decksabline2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(savedeck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(loaddeck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newdeck))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_showtextdeck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_transfer))
                    .addComponent(_deckname, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(_attrallcheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_attralluncheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(_allcolorcheck)
                            .addComponent(_decksab1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(_allcoloruncheck)
                            .addComponent(_textfilter, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_deckname, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_decksab, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_decksabline2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(savedeck)
                            .addComponent(loaddeck)
                            .addComponent(newdeck)
                            .addComponent(_reset))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(_showtextdeck)
                            .addComponent(_transfer))
                        .addGap(8, 8, 8)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(_clistsortno)
                    .addComponent(_clistsortcolor)
                    .addComponent(amount))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void _decknameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__decknameFocusGained
      _deckname.setSelectionStart(0);
      _deckname.setSelectionEnd(_deckname.getText().length());
}//GEN-LAST:event__decknameFocusGained

   private void newdeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newdeckActionPerformed
       mDeckPath = null;
       newDeck();
   }//GEN-LAST:event_newdeckActionPerformed

   private void loaddeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loaddeckActionPerformed
       loadDeck();

   }//GEN-LAST:event_loaddeckActionPerformed

   private void savedeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savedeckActionPerformed
       saveDeck();

   }//GEN-LAST:event_savedeckActionPerformed

   private void _allcoloruncheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__allcoloruncheckActionPerformed
       for (JCheckBox j : colors) {
           j.setSelected(false);
       }

       applyFilters();
}//GEN-LAST:event__allcoloruncheckActionPerformed

   private void _allcolorcheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__allcolorcheckActionPerformed
       for (JCheckBox j : colors) {
           j.setSelected(true);
       }

       applyFilters();
}//GEN-LAST:event__allcolorcheckActionPerformed

   private void quitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitActionPerformed
       if (JOptionPane.showConfirmDialog(null, Tools.getLoc(12), Tools.getLoc(13), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
           System.exit(0);
       }
   }//GEN-LAST:event_quitActionPerformed

   private void closeeditorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeeditorActionPerformed
       if (JOptionPane.showConfirmDialog(null, Tools.getLoc(14), "", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
           this.setVisible(false);
           this.dispose();
       }
   }//GEN-LAST:event_closeeditorActionPerformed

private void _clistKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event__clistKeyPressed
    synchronizeList(0);
    switch (evt.getKeyCode()) {
        case KeyEvent.VK_ADD:
        case KeyEvent.VK_RIGHT:
            addtoDeck();
            break;

        case KeyEvent.VK_SUBTRACT:
        case KeyEvent.VK_LEFT:
            removefromDeck();
            break;

    }
}//GEN-LAST:event__clistKeyPressed

private void _clistValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event__clistValueChanged
    if (_clist.getSelectedIndex() >= 0) {
        showSelected(_clist);
    }
}//GEN-LAST:event__clistValueChanged

private void _decklistValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event__decklistValueChanged
    if (_decklist.getSelectedIndex() >= 0) {
        showSelected(_decklist);
    }
}//GEN-LAST:event__decklistValueChanged

private void _decklistKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event__decklistKeyPressed
    synchronizeList(1);
    switch (evt.getKeyCode()) {
        case KeyEvent.VK_ADD:
        case KeyEvent.VK_RIGHT:
            addtoDeck();
            break;

        case KeyEvent.VK_SUBTRACT:
        case KeyEvent.VK_LEFT:
            removefromDeck();
            break;
    }
}//GEN-LAST:event__decklistKeyPressed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
}//GEN-LAST:event_formWindowClosed

private void _showtextdeckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__showtextdeckActionPerformed
// TODO add your handling code here:
    showTextDeck();
}//GEN-LAST:event__showtextdeckActionPerformed

private void _transferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__transferActionPerformed
// TODO add your handling code here:
    //transfer();
}//GEN-LAST:event__transferActionPerformed

private void _clistsortnoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event__clistsortnoItemStateChanged
    applyFilters();
    showFilteredList();
    showDeckList(-1);
}//GEN-LAST:event__clistsortnoItemStateChanged

private void _exportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__exportActionPerformed
}//GEN-LAST:event__exportActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    Tools.exportCardList(mCardList);
    _export.setSelected(false);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void _clistsortcolorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event__clistsortcolorItemStateChanged
    applyFilters();
    showFilteredList();
    showDeckList(-1);
}//GEN-LAST:event__clistsortcolorItemStateChanged

private void _attrallcheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__attrallcheckActionPerformed
    for (JCheckBox j : atkattrs) {
        j.setSelected(true);
    }
    for (JCheckBox j : defattrs) {
        j.setSelected(true);
    }
    applyFilters();
}//GEN-LAST:event__attrallcheckActionPerformed

private void _attralluncheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__attralluncheckActionPerformed
    for (JCheckBox j : atkattrs) {
        j.setSelected(false);
    }
    for (JCheckBox j : defattrs) {
        j.setSelected(false);
    }
    applyFilters();
}//GEN-LAST:event__attralluncheckActionPerformed

private void _checkruleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__checkruleActionPerformed
    try {
        String url = "http://docs.google.com/View?id=dztqbvp_64gndtdzd8";
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
    } catch (IOException ex) {
        Logger.getLogger(DeckEditorWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event__checkruleActionPerformed

private void _textfilterPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event__textfilterPropertyChange
}//GEN-LAST:event__textfilterPropertyChange

private void _resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__resetActionPerformed
    _allcolorcheck.doClick();
    _attralluncheck.doClick();
    _textfilter.setText("");
    applyFilters();
}//GEN-LAST:event__resetActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane _SCArea;
    private javax.swing.JButton _allcolorcheck;
    private javax.swing.JButton _allcoloruncheck;
    private javax.swing.JButton _attrallcheck;
    private javax.swing.JButton _attralluncheck;
    private javax.swing.JMenuItem _checkrule;
    private javax.swing.JList _clist;
    private javax.swing.ButtonGroup _clistsort;
    private javax.swing.JRadioButton _clistsortcolor;
    private javax.swing.JRadioButton _clistsortno;
    private javax.swing.JList _decklist;
    private javax.swing.JTextField _deckname;
    private javax.swing.JLabel _decksab;
    private javax.swing.JLabel _decksab1;
    private javax.swing.JLabel _decksabline2;
    private javax.swing.JMenu _export;
    private javax.swing.JButton _reset;
    private javax.swing.JButton _showtextdeck;
    private javax.swing.JTextField _textfilter;
    private javax.swing.JButton _transfer;
    private javax.swing.JLabel amount;
    private javax.swing.JMenuItem closeeditor5;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar6;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton loaddeck;
    private javax.swing.JButton newdeck;
    private javax.swing.JMenuItem quit5;
    private javax.swing.JButton savedeck;
    // End of variables declaration//GEN-END:variables
}
